package com.atguigu.gmall.common.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-17 18:03
 **/
@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     *
     * @param exchange
     * @param routingKey
     * @param message
     */
    public void send(String exchange, String routingKey, Object message) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
    }

    /**
     * 延迟队列发送时间
     * @param exchange
     * @param routingKey
     * @param message
     * @param delayTime
     */
    public void sendDelayed(String exchange, String routingKey, Object message, Integer delayTime) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message, message1 -> {
            message1.getMessageProperties().setDelay(delayTime * 1000);
            return message1;
        });
    }
}
